package com.intellagric.controller.module.trace_record_table;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.TraceTable;
import com.intellagric.pojo.TraceTableRows;
import com.intellagric.service.traceability.trace_record_table.TraceTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/2/22
 * @DESC： 表的数据操作
 */
@Controller
public class TraceTableController {

    @Autowired
    private TraceTableService traceTableService;

    /**
     * 添加记录表格信息
     * @Param tableName
     * @return ResponseMessage
     */
    @RequestMapping("/tracetable/insert")
    @ResponseBody
    public ResponseMessage add(String tableName,String produceBatchId,String processId,TraceTable table) {
        List<String> list = new ArrayList<>();
        list.add(UUID.randomUUID().toString().replaceAll("-",""));//id
        list.add(produceBatchId);//produceBatchId
        list.add(processId);//processId
        if (table.getFormData() != null) {
            for (String data:table.getFormData()) {
                list.add(data);
            }
            list.add("");//创建者
        } else {
            return ResponseMessage.fail();
        }
        return traceTableService.insertTraceTable(tableName,list);
    }


    /**
     * 删除记录表格信息
     * @Param id
     * @Param tableName
     * @return ResponseMessage
     */
    @RequestMapping("/tracetable/delete")
    @ResponseBody
    public ResponseMessage delete(String id,String tableName) {
        return traceTableService.deleteTraceTable(id, tableName);
    }

    /**
     * 修改记录表格信息
     * @Param table
     * @return ResponseMessage
     */
    @RequestMapping("/tracetable/edit")
    @ResponseBody
    public ResponseMessage edit(String id, String tableName, HttpServletRequest request) {
        StringBuilder sql = new StringBuilder("update " + tableName + " set ");
        Map<String, String[]> parameterMap = request.getParameterMap();
        for(Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            if (!entry.getKey().equals("tableName")) {
                sql.append(entry.getKey() + " = '" + entry.getValue()[0] + "',");
            }
        }
        sql.append("create_by = '' where id = '" + id + "'");
        String sqlStr = sql.toString();
        return traceTableService.updateTraceTable(sqlStr);
    }

    /**
     * 查询记录表格信息
     * @return Object
     */
    @RequestMapping("/tracetable/selectone")
    @ResponseBody
    public LayuiDataGridResult get(String id,String tableName) {
        return traceTableService.selectOneTraceTable(id, tableName);
    }

    /**
     * 查询记录表格信息
     * @return Object
     */
    @RequestMapping("/tracetable/select")
    @ResponseBody
    public LayuiDataGridResult get(int page,int limit,String tableName,String produceBatchId,String processId) {
        return traceTableService.selectTraceTable(page,limit,tableName,produceBatchId,processId);
    }

    /**
     * 查询记录表格树信息
     * @return LayuiDataGridResult
     */
    @RequestMapping("/tracetable/tree")
    @ResponseBody
    public LayuiDataGridResult getTree() {
        return null;
    }

    /**
     * 查询记录表格信息列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/tracetable/list")
    @ResponseBody
    public LayuiDataGridResult getList() {
        return null;
    }

    /**
     * 分页查询记录表格信息列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/tracetable/page")
    @ResponseBody
    public LayuiDataGridResult getPage() {
        return null;
    }

}
